#include <iostream>
#include <pthread.h>
#include <unistd.h>

using namespace std;

// 新线程
void *ThreadRoutine(void *arg)
{
    const char *threadname = (const char *)arg;
    while (1)
    {
        cout << "I am new thread" << ", pid : " << getpid() << " , " << threadname << endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    // 到这里就已经有进程了
    pthread_t pth1;
    pthread_create(&pth1, nullptr, ThreadRoutine, (void *)"thread 1");
    sleep(2);

    pthread_t pth2;
    pthread_create(&pth2, nullptr, ThreadRoutine, (void *)"thread 2");
    sleep(2);

    pthread_t pth3;
    pthread_create(&pth3, nullptr, ThreadRoutine, (void *)"thread 3");
    sleep(2);

    pthread_t pth4;
    pthread_create(&pth4, nullptr, ThreadRoutine, (void *)"thread 4");
    sleep(2);

    // 创建新进程以后，执行main函数的执行流我们叫做主线程
    while (1)
    {
        cout << "I am main thread" << ", pid : " << getpid() << endl;
        sleep(1);
    }
    return 0;
}